--[[
   @brief    popup annoucement when launch app
   @date     2019-08-16
--]]

app_launch_annouce_popup = {
    
    current_game_id = 0;
    current_uin = 0;
    continue_launch = false;
    dialog_id = Tenio.INVALID_CONTROLID;
    launch_tips_config = {};

    kMinWindowWidth = 400;
    kMinWindowHeight = 200;
    kMaxWindowWidth = 800;
    kMaxWindowHeight = 600;
    kDefaultWindowWidth = 490;
    kDefaultWindowHeight = 270;

    kPopupUIUrl = LoadStr("APP_PRELAUNCH_POPUP_URL");

    init = function()
        base_utility.log("[app_launch_annouce_popup]init. ");
        comm_center.reg_service("i_service_get_app_prelaunch_tips_config",
                                "app_launch_annouce_popup.on_get_app_prelaunch_config");
        comm_center.reg_service("i_service_check_and_show_app_prelaunch_tips",
                                "app_launch_annouce_popup.on_check_prelaunch_tips");
        comm_center.reg_service("i_service_close_app_prelaunch_tips",
                                "app_launch_annouce_popup.on_close_prelaunch_tips");
    end;

    request_config = function(index)
        base_utility.log("[app_launch_annouce_popup]request_config. ");

        if nil == index then
            index = 0;
        end

        local client_type = 1;
        if base_utility.is_type_of_oversea_ver() then
            client_type = 2;
        end

        local post_data = {
                data_names = "app_launch_tips_config",
                command = "list_all",
                params = {
                    start_page = index,
                    items_per_pager = 50
                },
                stamp = {
                    credential = {
                        agent_client_type = client_type,
                    },
                    agent_client_language = base_utility.get_currrent_language_str();
                }
        };
        local url = LoadStr("GL_GAME_CONFIG_URL");
        base_utility.curl_request_async(url
            , "app_launch_annouce_popup.on_request_config"
            , false, nil, true, nil, base_utility.table_to_json(post_data));
    end;

    on_request_config = function(rsp)
        base_utility.log("[app_launch_annouce_popup]config rsp".. rsp);
        local res_tbl = base_utility.json_to_table(rsp);
        if nil == res_tbl or nil == res_tbl.result then
            return;
        end
        if 0 ~= tonumber(res_tbl.result.error_code) then
            return;
        end

        local items_count = 0;
        if nil ~= res_tbl.items and type(res_tbl.items) == "table" then
            items_count = #res_tbl.items;
            for k, v in pairs(res_tbl.items) do
                table.insert(app_launch_annouce_popup.launch_tips_config, v);
            end
        end

        -- request more
        local total_items = tonumber(res_tbl.total_items or "0");
        local start_page = tonumber(res_tbl.start_page or "0");
        local items_per_page = tonumber(res_tbl.items_per_pager or "0");

        if total_items > (start_page * items_per_page + items_count) then
            app_launch_annouce_popup.request_config(start_page + 1);
        end
    end;

    on_set_app_prelaunch_config = function(param)
        if nil == param or nil == param.game_id then
            return;
        end

        local config = app_launch_annouce_popup.get_tips_config(param.game_id);
        if nil == config then
            return;
        end

        local loc_config = {};
        loc_config.title = config.title or "";
        loc_config.content = config.content or "";

        local data = {};
        data["common"] = 0;
        data["key"] = "app.prelaunch.tips."..tostring(param.game_id);
        if true == param.do_not_show then
            loc_config.do_not_show = true;
        else
            loc_config.do_not_show = false;
        end
        data["value"] = base_utility.table_to_json(loc_config);
        comm_center.call_service("Svr_SetConfig", data);
    end;

    on_get_app_prelaunch_config = function()
        local config = {};
        local game_id = app_launch_annouce_popup.current_game_id;
        config["game_id"] = game_id;
        local tips_cfg = app_launch_annouce_popup.get_tips_config(game_id);
        if nil ~= tips_cfg then
            config["title"] = tips_cfg["title"] or "";
            config["content"] = tips_cfg["content"] or "";
        end
        return config;
    end;

    on_check_prelaunch_tips = function(param)
        if nil == param then
            return;
        end

        param.is_show_tips = false;

        local config = app_launch_annouce_popup.get_tips_config(param.game_id);
        if nil == config then
            return param;
        end

        if not app_launch_annouce_popup.need_show_prelaunch_tips(param.game_id) then
            return param;
        end
        
        app_launch_annouce_popup.current_game_id = param.game_id;
        app_launch_annouce_popup.current_uin = param.uin;
        app_launch_annouce_popup.continue_launch = false;

        local win_width = app_launch_annouce_popup.kDefaultWindowWidth;
        local win_height = app_launch_annouce_popup.get_window_adjust_height(config);

        if nil ~= config.width and "" ~= config.width and "0" ~= config.width then
            win_width = tonumber(config.width);
        end

        if nil ~= config.height and "" ~= config.height and "0" ~= config.height then
            win_height = tonumber(config.height);
        end

        if win_width < app_launch_annouce_popup.kMinWindowWidth then
            win_width = app_launch_annouce_popup.kDefaultWindowWidth;
        elseif win_width > app_launch_annouce_popup.kMaxWindowWidth then
            win_width = app_launch_annouce_popup.kDefaultWindowWidth;
        end

        if win_height < app_launch_annouce_popup.kMinWindowHeight then
            win_height = app_launch_annouce_popup.kDefaultWindowHeight;
        elseif win_height > app_launch_annouce_popup.kMaxWindowHeight then
            win_height = app_launch_annouce_popup.kDefaultWindowHeight;
        end

        local web_param = {
            url = app_launch_annouce_popup.kPopupUIUrl,
            use_ptlogin = false,
            width = win_width,
            height = win_height,
            modal = true,
            dark_close_btn = true,
            resizable = false,
            show_loading_page = false,
            caption = LoadStr("POP_WND_COMMON_TITLE"),
            close_window = "i_service_close_app_prelaunch_tips"
        };
        app_launch_annouce_popup.dialog_id = 
                comm_center.call_service("i_service_show_simple_web_window", web_param);
        param.is_show_tips = true;
        return param;
    end;

    get_tips_config = function(game_id)
        if nil == game_id then 
            return nil; 
        end
        local id = tostring(game_id);
        for k, v in pairs(app_launch_annouce_popup.launch_tips_config) do
            if id == tostring(v["game_id"] or "") then
                return v;
            end
        end
        return nil;
    end;

    need_show_prelaunch_tips = function(game_id)
        if nil == game_id then
            return false;
        end
        local cur_config = app_launch_annouce_popup.get_tips_config(game_id);
        if nil == cur_config then
            return false;
        end
        local data = {};
        data["common"] = 0;
        data["key"] = "app.prelaunch.tips."..tostring(game_id);
        local loc_cfg = comm_center.call_service("Svr_GetConfig", data);
        if nil ~= loc_cfg and 1 == loc_cfg.exits then
            local val = loc_cfg.value or "{}";
            local val_tbl = base_utility.json_to_table(val);
            if nil ~= val_tbl and val_tbl.title == cur_config.title and
                val_tbl.content == cur_config.content and
                val_tbl.do_not_show == true then
                    return false;
            end
        end
        return true;
    end;

    on_close_prelaunch_tips = function(param)
        local continue_launch = false;
        if nil ~= param and true == param.continue_launch then
           continue_launch = true;
        end
        if nil == param.id then -- call from js(external)
           app_launch_annouce_popup.continue_launch = continue_launch;
           if true == param.do_not_show then
                local save_param = {
                    game_id = app_launch_annouce_popup.current_game_id;
                    do_not_show = param.do_not_show;
                }
                app_launch_annouce_popup.on_set_app_prelaunch_config(save_param);
            end
           -- close popup window
           local close_param = {
                id = app_launch_annouce_popup.dialog_id;
           };
           comm_center.call_service("i_service_close_simple_web_window", close_param);
           return;
        end

        local data = {
            game_id = app_launch_annouce_popup.current_game_id;
            uin = app_launch_annouce_popup.current_uin;
            continue_action = app_launch_annouce_popup.continue_launch;
        };
        comm_center.call_service("Svr_AppPreLaunchTipsContinueLaunch", data);
    end;

    calc_utf8_len = function(text)
        local count = 0;
        if type(text) == "string" then
            local left = string.len(text);
            local arr  = {0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc};
            while left ~= 0 do
                local tmp = string.byte(text, -left);
                local i   = #arr;
                while arr[i] do
                    if tmp >= arr[i] then
                        left = left - i
                        break
                    end
                    i = i - 1
                end
                count = count + 1;
            end
        end
        return count;
    end;

    get_window_adjust_height = function(config)
        if nil == config then
            return app_launch_annouce_popup.kDefaultWindowHeight;
        end
        
        local text = config.content or "";
        local text_len = app_launch_annouce_popup.calc_utf8_len(text);
        if text_len < 150 then
            return app_launch_annouce_popup.kDefaultWindowHeight;
        else
            local delta_height = math.ceil((text_len - 150) / 30) * 20;
            return app_launch_annouce_popup.kDefaultWindowHeight + delta_height;
        end
    end;

}

app_launch_annouce_popup.init();